/*
 * Copyright (C) 2016 The Fosafer Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fosafer.idcard_demo.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.fosafer.idcard_demo.R;
import com.fosafer.idcard_demo.util.AppManagerUtil;
import com.fosafer.idcard_demo.util.DensityUtil;
import com.fosafer.idcard_demo.view.CustomProgressDialog;


/**
 * Create By :   IDEA
 * Package:      [com.fosafer.idcard_demo.act]
 * ClassName:    [BaseAct]
 * Description:  [基类Act]
 * Author:       [jianzhic]
 * CreateDate:   [16-4-20 15:47]
 * UpdateUser:   [jianzhic]
 * UpdateDate:   [16-4-20 15:47]
 * UpdateRemark: [Update Description]
 * Version:      [v1.0.0]
 * <p>
 * Copyright (c) 2016 Fosafer Co.,Ltd. All rights reserved.
 */
public abstract class BaseAct extends Activity {

    /***
     * 上下文.
     */
    protected Context mContext;
    private AlertDialog baseTipSmallDialog;
    private AlertDialog tipAttendanceOkDialog;
    /**
     * 自定义等待框.
     */
    private CustomProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewLayoutId());
        mContext = this;
        AppManagerUtil.getInstance().addAct(this);

        initView();
        setOnClick();
        bindData();
    }


    /**
     * startAct.
     *
     * @param cls cls
     */

    void startAct(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }


    /**
     * skip.
     *
     * @param clazz       Target class
     * @param isCloseSelf isCloseSelf
     */
    void skip(Class<?> clazz, boolean isCloseSelf) {
        startAct(clazz);
        if (isCloseSelf) {
            ((Activity) mContext).finish();
        }
    }


    /**
     * @param keys        keys
     * @param strValues   strValues
     * @param clazz       clazz
     * @param isCloseSelf is close Self
     */
    public void skip(String[] keys, String[] strValues, Class<?> clazz,
                     boolean isCloseSelf) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        for (int i = 0; i < keys.length; i++) {
            String key = keys[i];
            String obj = strValues[i];
            if (!TextUtils.isEmpty(key) && obj != null) {
                intent.putExtra(key, obj);
            }
        }
        startActivity(intent);
        if (isCloseSelf) {
            ((Activity) mContext).finish();
        }
    }


    /**
     * 初始化等待框.
     */
    private void initProgressDialog() {
        mProgressDialog = new CustomProgressDialog(AppManagerUtil.getInstance().currentAct(), R.style
                .custom_dialog_normal);
    }

    /**
     * 显示等待框.
     */
    public void showProgressDialog() {
        initProgressDialog();
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            disMissProgressDialog();
            mProgressDialog.show();
        } else {
            if (mProgressDialog != null) {
                mProgressDialog.show();
            }
        }
    }


    /***
     * 隐藏等待框.
     */
    public void disMissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        disMissProgressDialog();
        AppManagerUtil.getInstance().removeAct(this);
    }

    /**
     * show tips dialog
     *
     * @param title           title
     * @param msg             msg
     * @param onClickListener click listener
     */
    public void showTipDialog(String title, String msg, View.OnClickListener onClickListener) {
        baseTipSmallDialog = new AlertDialog.Builder(AppManagerUtil.getInstance().currentAct()).create();
        baseTipSmallDialog.show();
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_small, null);
        baseTipSmallDialog.setContentView(view);
        baseTipSmallDialog.setCancelable(false);
        WindowManager.LayoutParams params = baseTipSmallDialog.getWindow().getAttributes();
        params.width = DensityUtil.getScreenWidth() / 2 + 300;
        baseTipSmallDialog.getWindow().setAttributes(params);
        TextView layout_dialog_small_tv_title = (TextView) view.findViewById(R.id.layout_dialog_small_tv_title);
        TextView layout_dialog_small_tv_msg = (TextView) view.findViewById(R.id.layout_dialog_small_tv_msg);
        TextView layout_dialog_small_tv_ok = (TextView) view.findViewById(R.id.layout_dialog_small_tv_ok);
        TextView layout_dialog_small_tv_cancel = (TextView) view.findViewById(R.id.layout_dialog_small_tv_cancel);
        layout_dialog_small_tv_ok.setOnClickListener(onClickListener);
        layout_dialog_small_tv_cancel.setOnClickListener(onClickListener);
        layout_dialog_small_tv_title.setText(title);
        layout_dialog_small_tv_msg.setText(msg);

    }

    /**
     * 确认弹出框.
     *
     * @param title title
     * @param msg   msg
     */
    public void showEnsureDialog(Context context, String title, String msg, View.OnClickListener onClickListener) {
        tipAttendanceOkDialog = new AlertDialog.Builder(context).create();
        tipAttendanceOkDialog.show();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog_verify_ok, null);
        tipAttendanceOkDialog.setContentView(view);
        tipAttendanceOkDialog.setCancelable(false);
        WindowManager.LayoutParams params = tipAttendanceOkDialog.getWindow().getAttributes();
        params.width = DensityUtil.getScreenWidth() * 3 / 4;
        tipAttendanceOkDialog.getWindow().setAttributes(params);
        TextView layout_dialog_verify_ok_tv_title = (TextView) view.findViewById(R.id
                .layout_dialog_verify_ok_tv_title);
        TextView layout_dialog_verify_ok_tv_msg = (TextView) view.findViewById(R.id
                .layout_dialog_verify_ok_tv_msg);
        TextView layout_dialog_verify_ok_tv_ok = (TextView) view.findViewById(R.id
                .layout_dialog_verify_ok_tv_ok);
        layout_dialog_verify_ok_tv_ok.setOnClickListener(onClickListener);
        layout_dialog_verify_ok_tv_title.setText(title);
        layout_dialog_verify_ok_tv_msg.setText(msg);
    }


    /**
     * 取消确认弹出框
     */
    public void disEnsureDialog() {
        if (tipAttendanceOkDialog != null && tipAttendanceOkDialog.isShowing()) {
            tipAttendanceOkDialog.dismiss();
        }
    }

    /**
     * 取消弹出框
     */
    public void disTipDialog() {
        if (baseTipSmallDialog != null && baseTipSmallDialog.isShowing()) {
            baseTipSmallDialog.dismiss();
        }
    }


    protected void initView() {

    }


    protected void setOnClick() {

    }

    protected void bindData() {

    }

    protected abstract int getContentViewLayoutId();
}
